﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Vancl.ERP.Model.TMS;
using Vancl.ERP.BLL.TMS;

namespace Vancl.TMS.Test
{
    [TestFixture]
    public class VerifyAssignRule
    {
        List<DeliveryView> _view = new List<DeliveryView>();
        List<DeliveryView> _view2 = new List<DeliveryView>();
        AssignDeliveryOrder _mgr = new AssignDeliveryOrder();

        private void CreateData()
        {
            _view.Clear();

            DeliveryView v = new DeliveryView();
            v.OrderType = EnumOrderType.Order;
            v.HouseCode = "北京";
            v.AttributeName = "通路";
            v.InterCityLineCity = "广州";
            _view.Add(v);

            v = new DeliveryView();
            v.OrderType = EnumOrderType.Order;
            v.HouseCode = "北京";
            v.AttributeName = "通路";
            v.InterCityLineCity = "广州";
            _view.Add(v);

            v = new DeliveryView();
            v.OrderType = EnumOrderType.Order;
            v.HouseCode = "北京";
            v.AttributeName = "通路";
            v.InterCityLineCity = "广州";
            _view.Add(v);

            v = new DeliveryView();
            v.OrderType = EnumOrderType.RedeployD;
            v.HouseCode = "北京";
            v.AttributeName = "通路";
            v.InterCityLineCity = "广州";
            _view.Add(v);

            v = new DeliveryView();
            v.OrderType = EnumOrderType.Order;
            v.HouseCode = "上海";
            v.AttributeName = "通路";
            v.InterCityLineCity = "广州";
            _view.Add(v);

            v = new DeliveryView();
            v.OrderType = EnumOrderType.Order;
            v.HouseCode = "北京";
            v.AttributeName = "通路2";
            v.InterCityLineCity = "广州";
            _view.Add(v);

            v = new DeliveryView();
            v.OrderType = EnumOrderType.Order;
            v.HouseCode = "北京";
            v.AttributeName = "通路";
            v.InterCityLineCity = "汕头";
            _view.Add(v);

            v = new DeliveryView();
            v.OrderType = EnumOrderType.Order;
            v.HouseCode = "北京";
            v.AttributeName = "通路";
            v.InterCityLineCity = null;
            _view.Add(v);

            v = new DeliveryView();
            v.OrderType = EnumOrderType.Order;
            v.HouseCode = "北京";
            v.AttributeName = "通路";
            v.InterCityLineCity = null;
            _view.Add(v);
        }

        private void InitChk()
        {
            foreach (DeliveryView v in _view)
            {
                v.CHK = false;
            }
        }

        [SetUp]
        public void Init()
        {
            CreateData();
            Assert.IsTrue(_view.Count > 0);
        }

        [Test, ExpectedException]
        public void AssignDeliveryOrderNoTest_null_CHK()
        {
            _mgr.ViewList = null;
            Assert.AreEqual(false, _mgr.CanToDo());

            _mgr.ViewList = _view2;
            Assert.AreEqual(false, _mgr.CanToDo());

            //
            InitChk();
            _mgr.ViewList = _view;
            Assert.AreEqual(false, _mgr.CanToDo());
        }

        [Test]
        public void AssignDeliveryOrderTest()
        {
            //
            for (int i = 0; i < _view.Count; i++)
            {
                InitChk();
                _view[i].CHK = true;
                _mgr.ViewList = _view;
                if (string.IsNullOrEmpty(_view[i].HouseCode)
                    || string.IsNullOrEmpty(_view[i].AttributeName)
                    || string.IsNullOrEmpty(_view[i].InterCityLineCity))
                {
                    Assert.AreEqual(false, _mgr.CanToDo());
                }
                else
                {
                    Assert.AreEqual(true, _mgr.CanToDo());
                }
            }

            //
            for (int i = 0; i < _view.Count; i++)
            {
                _view[i].CHK = true;
            }
            _mgr.ViewList = _view;
            Assert.AreEqual(false, _mgr.CanToDo());

            //
            InitChk();
            _view[0].CHK = true;
            _view[2].CHK = true;
            _mgr.ViewList = _view;
            Assert.AreEqual(true, _mgr.CanToDo());

            //
            InitChk();
            _view[1].CHK = true;
            _view[3].CHK = true;
            _mgr.ViewList = _view;
            Assert.AreEqual(false, _mgr.CanToDo());

            //
            InitChk();
            _view[5].CHK = true;
            _view[6].CHK = true;
            _mgr.ViewList = _view;
            Assert.AreEqual(false, _mgr.CanToDo());

            InitChk();
            _view[7].CHK = true;
            _view[8].CHK = true;
            _mgr.ViewList = _view;
            Assert.AreEqual(false, _mgr.CanToDo());
        }

        [Test]
        public void CheckAllSelectTest()
        {
            string msg;
            bool allSelect = false;

            _mgr.ViewList = null;
            allSelect = _mgr.CheckAllSelect(out msg);
            Assert.AreEqual(true, allSelect);
            Console.Out.Write(msg);

            _mgr.ViewList = _view2;
            allSelect = _mgr.CheckAllSelect(out msg);
            Assert.AreEqual(true, allSelect);
            Console.Out.Write(msg);

            InitChk();
            _mgr.ViewList = _view;
            allSelect = _mgr.CheckAllSelect(out msg);
            Assert.AreEqual(true, allSelect);
            Console.Out.Write(msg);

            InitChk();
            _view[0].CHK = true;
            _mgr.ViewList = _view;
            allSelect = _mgr.CheckAllSelect(out msg);
            Assert.AreEqual(false, allSelect);
            Console.Out.Write(msg);

            InitChk();
            _view[0].CHK = true;
            _view[1].CHK = true;
            _view[2].CHK = true;
            _mgr.ViewList = _view;
            allSelect = _mgr.CheckAllSelect(out msg);
            Assert.AreEqual(true, allSelect);
            Console.Out.Write(msg);

            InitChk();
            _view[3].CHK = true;
            _mgr.ViewList = _view;
            allSelect = _mgr.CheckAllSelect(out msg);
            Assert.AreEqual(true, allSelect);
            Console.Out.Write(msg);
        }
    }
}
